﻿
cloud_game_protocol_control = {
    root_id_ = 0;
    root_ptr_ = nil;
    list_ptr_ = nil;
    game_id_ = 0;
    game_ctrl_id_2_prtcl_info = {};
    protocol_status_arry_ = {};
};


function cloud_game_protocol_control:init()
    base_utility.log("[cloud_game_protocol_control] init ");
    comm_center.listen_broadcast("i_broadcast_game_protocol_update_suc", 
                "cloud_game_protocol_control:OnGameProtocolUpdateSuc");

    comm_center.listen_broadcast("i_broadcast_ui_mode_change", 
                "cloud_game_protocol_control:OnUiModeChange");
end

function cloud_game_protocol_control:inner_log(msg, core_msg)
    base_utility.log(string.format("[cloud_game_protocol_control] %s", msg), core_msg);
end;

function cloud_game_protocol_control:CheckCloudGameProtocol(game_id)
    if game_id == nil then
        return true;
    end;

    if false == frame_bottom_model.is_all_protocol_agree(game_id) then
        self.game_id_ = tonumber(game_id);
        self:ShowCloudGameProtocolWnd(true);
        frame_bottom_view.show_protocol_tips(false);
        return false;
    end;
    return true;
end;

function cloud_game_protocol_control:ShowCloudGameProtocolWnd(bShow)
    if bShow == true then
        if self.root_ptr_ ~= nil then
            self.root_ptr_:Restore();
            self.root_ptr_:MoveToCenter();
            self.root_ptr_:BringToTop();
        else
            self:Create();
            self:RefreshContent();
        end;
    else
        self:Close();
    end
end

function cloud_game_protocol_control:Create()
    if self.root_ptr_ ~= nil then
        return true;
    end
    --local main_wnd_id = ui_utility.get_ctrl_id_by_cid(CID_MAINFRAME_Dialog_MainFrame);
    --self.root_id_ = layout_mgr.restore_sub_layout(main_wnd_id, "cloud_game_protocol.tpf");
    self.root_id_ = layout_mgr.restore_layout("cloud_game_protocol.tpf");
    if self.root_id_ ~= 0 then
        self.root_ptr_ = ui_utility.get_ctrl_ptr_by_id(self.root_id_);
        if self.root_ptr_ ~= nil then
            self.root_ptr_:MoveToCenter();
            self.root_ptr_:BringToTop();
        end
        --ui_utility.do_fake_modal_by_id(self.root_id_);
        return true;
    else
        return false;
    end
end

function cloud_game_protocol_control:RefreshContent()
    if self.root_id_ == 0 then
        return;
    end;
    self.list_ptr_ = ui_utility.get_ctrl_ptr_by_path(self.root_id_, "ProtocolList");
    if nil == self.list_ptr_ then
        return;
    end
    local highlight_color = "RGB:255,200,0";
    if base_utility.is_type_of_oversea_ver() then
        highlight_color = "RGB:25,178,255";
    end
    
    local info = frame_bottom_model.get_game_user_protocol(self.game_id_);
    if nil == info or nil == info.protocol_list then
        return;
    end
    
    local item_count = #info.protocol_list;
    ui_utility.setproperty_by_id(self.root_id_, "Height", tostring(125 + item_count * 22));
    self.list_ptr_:SetItemCount(item_count);
    local protocal_id = 0;
    for id = 0, item_count - 1 do
        local item_id = self.list_ptr_:GetItemRootIDByIdx(id);
        protocal_id = info.protocol_list[id + 1].id;
        self.game_ctrl_id_2_prtcl_info[item_id] = info.protocol_list[id + 1];

        local protocol_btn_id = ui_utility.get_ctrl_id_by_path(item_id, "Btn_JumpUrl");
        ui_utility.setproperty_by_id(protocol_btn_id, "Caption", info.protocol_list[id + 1].name);
        ui_utility.setproperty_by_id(protocol_btn_id, "TextColor", highlight_color);
        ui_utility.setproperty_by_id(protocol_btn_id, "TextColorHover", highlight_color);
        ui_utility.setproperty_by_id(protocol_btn_id, "TextColorPressed", highlight_color);
        ui_utility.setproperty_by_id(
                    protocol_btn_id, "OnClick", "cloud_game_protocol_control:OnClickUserProtocolBtn");

        local protocol_check_btn_id = ui_utility.get_ctrl_id_by_path(item_id, "CbxUserProtocol");
        ui_utility.setproperty_by_id(
                    protocol_check_btn_id, "OnSignaled", "cloud_game_protocol_control:OnUserProtocolSignaled");
        ui_utility.setproperty_by_id(
                    protocol_check_btn_id, "OnUnSignaled", "cloud_game_protocol_control:OnUserProtocolUnsignaled");
        local protocol_check_btn_ptr = ui_utility.get_ctrl_ptr_by_path(item_id, "CbxUserProtocol");
        local is_check = FALSE;
        if nil ~= protocol_check_btn_ptr then
            if frame_bottom_model.is_protocol_check(self.game_id_, info.protocol_list[id + 1].id) then
                is_check = TRUE;
            end
            protocol_check_btn_ptr:SetCheckedNoSignal(is_check);
        end

        if is_check == TRUE then
            self.protocol_status_arry_[protocal_id] = true;
        else
            self.protocol_status_arry_[protocal_id] = false;
        end;
    end
    self:UpdateOkBtnState();
end;

function cloud_game_protocol_control:Close()
    if self.root_ptr_ ~= nil then
        ----shadow_helper.destroy_shadow(self.root_id_);
        self.root_ptr_:Show(FALSE);
    end
    --ui_utility.end_fake_modal_by_id(self.root_id_);
    layout_mgr.delay_remove_ctrl(self.root_id_);
    self:ClearInfo();
    --[[if self.shadow_ptr_ ~= nil then
        self.shadow_ptr_:Show(FALSE);
    end
    layout_mgr.delay_remove_ctrl(self.shadow_id_);
    self.shadow_id_ = 0;
    self.shadow_ptr_ = nil; --]]
end

function cloud_game_protocol_control:Confirm()
    self:SaveSelection();
    self:Close();
end

function cloud_game_protocol_control:Cancel()
    self:Close();
end

function cloud_game_protocol_control:OnUserProtocolSignaled(uilib, control_id)
    local item_id = ui_utility.get_ctrl_parent_id(control_id);
    local item_info = self.game_ctrl_id_2_prtcl_info[item_id];
    if nil ~= item_info then
        self.protocol_status_arry_[tonumber(item_info.id)] = true;
    end
    self:UpdateOkBtnState();
end;

function cloud_game_protocol_control:OnUserProtocolUnsignaled(uilib, control_id)
    local item_id = ui_utility.get_ctrl_parent_id(control_id);
    local item_info = self.game_ctrl_id_2_prtcl_info[item_id];
    if nil ~= item_info then
        self.protocol_status_arry_[tonumber(item_info.id)] = false;
    end
    self:UpdateOkBtnState();
end;

function cloud_game_protocol_control:OnClickUserProtocolBtn(uilib, control_id)
    local item_id = ui_utility.get_ctrl_parent_id(control_id);
    local item_info = self.game_ctrl_id_2_prtcl_info[item_id];
    if nil ~= item_info and nil ~= item_info.url and "" ~= item_info.url then
        web_utility.open_new_browser(item_info.url, true, false, true);
    end
end;

function cloud_game_protocol_control:SaveSelection()
    if self.protocol_status_arry_ == nil then
        return;
    end;
    frame_bottom_model.update_protocol_agree_table_by_gameid(
                                            self.game_id_, self.protocol_status_arry_);
    local tbl_data = frame_bottom_control.get_game_and_plugin_state_tbl(self.game_id_);
    if true == frame_bottom_control.table_is_empty(tbl_data) then return end
    frame_bottom_view.update_game_or_plugin_state(tbl_data);

     local launch_param = {game_id = self.game_id_,ignore_time = true};
     comm_center.call_service("SvrLua_CloudGame_Launch_Request", launch_param);
end;

function cloud_game_protocol_control:ClearInfo()
    self.root_id_ = 0;
    self.root_ptr_ = nil;
    self.list_ptr_ = nil;
    self.game_id_ = 0;
    self.game_ctrl_id_2_prtcl_info = {};
    self.protocol_status_arry_ = {};
end;

function cloud_game_protocol_control:OnGameProtocolUpdateSuc(data)
    self:RefreshContent();
end;

function cloud_game_protocol_control:OnUiModeChange(data)
    if data == nil or data.bigpic_ui == nil then
        return;
    end
    if data.bigpic_ui == 0 then
        return;
    end
    self:ShowCloudGameProtocolWnd(false);
end;

function cloud_game_protocol_control:UpdateOkBtnState()
    local ok_btn_id = ui_utility.get_ctrl_id_by_path(self.root_id_, "OKBtn");
    local item_count = #self.protocol_status_arry_;
    for id = 0, item_count - 1 do
        if self.protocol_status_arry_[id + 1] == false then
            if ok_btn_id ~= nil then
                ui_utility.setproperty_by_id(ok_btn_id, "Disable", "true");
            end;
            return;
        end;
    end;
    if item_count > 0 then
        if ok_btn_id ~= nil then
            ui_utility.setproperty_by_id(ok_btn_id, "Disable", "false");
        end;
    end;
end;

cloud_game_protocol_control.init();
